import React, { useContext, useMemo, useState } from 'react';
import { ConractControllerContext, IContractsActionEnum } from '../common';
import { Drawer, Table } from 'antd';
import { openCounterpartColumns } from '../mconfig';
import Cls from 'classnames';
import { ContractManageAPI } from '../common/type';
import { CounterpartDetailDrawer } from './CounterpartDetailDrawer';

// import { TipsErrorModalNew } from '@/components/TipsErrorModalNew';

export const UpdateCounterpartView: React.FC<ContractManageAPI.IUpdateCounterpartView> = ({ openCounterpartModal }) => {
  /** 引入上下文 */
  const { pageType, counterpartRows, contractDispatch, contarctJsonList, contractDetailInfo } = useContext(ConractControllerContext);

  /**是否打开相对方详情抽屉 */
  const [showInfoDrawer, setShowInfoDrawer] = useState<Record<string, any>>({
    isShowDrawer: false,
    showInfoObj: {},
  });

  /** 删除选中数据 */
  const delSelectRow = (selectObj: Record<string, any>) => {
    const newList: Record<string, any>[] = counterpartRows?.filter((item: any) => {
      return item?.oppositeID != selectObj?.oppositeID;
    });
    if (selectObj && selectObj?.oppositeID) {
      const oldContractList: Record<string, any>[] =
        contractDetailInfo?.['oppositeList'] && contractDetailInfo?.['oppositeList']?.length > 0 ? contractDetailInfo?.['oppositeList'] : [];
      const oldJsonContractList: Record<string, any>[] =
        contarctJsonList?.['oppositeList'] && contarctJsonList?.['oppositeList']?.length > 0 ? contarctJsonList?.['oppositeList'] : [];
      let delList: Record<string, any>[] = [];

      const oldInfo = oldContractList?.find((itemInfo: any) => itemInfo?.oppositeID == selectObj?.oppositeID);

      if (!(oldInfo && oldInfo?.oppositeID)) {
        let opposList = oldJsonContractList?.filter((itemJson: any) => itemJson?.oppositeID != selectObj?.oppositeID);
        contractDispatch({
          type: IContractsActionEnum.CONTRACT_JSONLIST,
          payload: {
            ...contarctJsonList,
            oppositeList: opposList,
          },
        });
      } else {
        oldContractList?.forEach((item: any) => {
          if (item?.oppositeID == selectObj?.oppositeID) {
            delList = oldJsonContractList?.filter((itemFil: any) => itemFil?.oppositeID != selectObj?.oppositeID);
            delList?.push({
              ...selectObj,
              modifyFlag: '2',
            });
          }
        });
        contractDispatch({
          type: IContractsActionEnum.CONTRACT_JSONLIST,
          payload: {
            ...contarctJsonList,
            oppositeList: delList,
          },
        });
      }
    }

    contractDispatch({
      type: IContractsActionEnum.SELECT_COUNTERPARTROWS,
      payload: newList,
    });
  };

  /** 表格行配置 */
  const columns: Record<string, any>[] = useMemo(() => {
    return openCounterpartColumns(openCounterpartModal, delSelectRow, true, setShowInfoDrawer, pageType);
  }, [counterpartRows]);

  return (
    <>
      <div className={Cls('list-table', 'descriptionsType-tableSty')}>
        <Table
          bordered
          size="small"
          dataSource={counterpartRows}
          columns={columns}
          rowKey={(res) => res?.oppositeID}
          scroll={{ x: 1200, y: '' }}
          pagination={false}
        />
      </div>

      {/* 相对方详情 */}
      <CounterpartDetailDrawer
        openDrawer={showInfoDrawer?.isShowDrawer}
        dataInfo={showInfoDrawer?.showInfoObj}
        onClose={() => {
          setShowInfoDrawer({
            isShowDrawer: false,
            showInfoObj: {},
          });
        }}
      />

      {/* 禁用提示框 */}
      {/* {tipsModalObj?.isShowModal && (
        <TipsErrorModalNew
          // ref={tipErrorModalRef}
          modalLoading={false}
          onClose={() => {
            setTipsModalObj({
              isShowModal: false,
              selectRowInfo: {},
            });
          }}
          modalOnOK={onSaveCloseCounterpart}
          tipTextColor="#000"
          modalTip={
            <div>
              确认取消选中【<span style={{ color: 'red' }}>{tipsModalObj?.selectRowInfo?.oppositeName || '--'}</span>】相对方?
            </div>
          }
        />
      )} */}
    </>
  );
};
